#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

void solve() {
	ll a, b, n, m; cin >> a >> b >> n >> m;
	if ((m + 1) * b <= m * a) {
		cout << n * b << endl;
		return;
	}
	ll tmp = n / (m + 1);
	ll ans = tmp * m * a;
	ans += n % (m + 1) * min(a, b);
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}